<?php


namespace app\shopConsole\service;

use app\dao\user\ShopAdminDao;
use app\lib\exception\AuthException;
use app\utils\JWT;
use think\facade\Cache;

class AuthService
{
    public function login($username, $password)
    {
        $shopAdminDao = new ShopAdminDao();

        $admin = $shopAdminDao->getByUsername($username);

        if ($admin == null || !$admin->validatePassword($password))
        {
            throw new AuthException('账号或密码错误');
        }
        if ($admin->is_ban == 1 || $admin->shop->is_ban == 1)
        {
            throw new AuthException('账号被禁用');
        }

        //生成token
        $token = JWT::create($admin->id);

        $this->setToken($token, $admin);

        return $token;
    }

    public function setToken($token, $admin)
    {
        Cache::store('redis')->set($token, $admin, config('jwt.exp'));
    }

    public function getToken($token)
    {
        return Cache::store('redis')->get($token);
    }

}